<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="生产工单编码" prop="workorderCode ">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入生产工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="销售订单号" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          placeholder="请输入销售订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品编码" prop="productCode ">
        <el-input
          v-model="queryParams.productCode "
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="日期范围">
        <!-- <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker> -->
        <el-date-picker
          v-model="timeValue"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleInput"
        >导入</el-button>
      </el-col>
    </el-row>

    <vxe-table
      border
      stripe
      ref="tableRef"
      size="small"
      header-align="center"
      :row-config="rowConfig"
      :column-config="columnConfig"
      @checkbox-change="checkboxChangeEvent"
      @checkbox-all="checkboxAll"
      :data="tableData"
    >
      <!-- <vxe-column type="checkbox" width="40" fixed="left"></vxe-column> -->
      <vxe-column field="workorderCode" drag-sort show-overflow="title" align="center" title="工单编号"></vxe-column>
      <vxe-column field="productCode" width="100" show-overflow="title" align="center" title="产品单号"></vxe-column>
      <vxe-column field="productName" width="100" show-overflow="title" align="center" title="产品名称"></vxe-column>
      <vxe-column field="specification" show-overflow="title" align="center" title="产品规格" ></vxe-column>
      <vxe-column field="orderCode" show-overflow="title" align="center" title="销售订单号"></vxe-column>
      <vxe-column field="clientName" width="100" show-overflow="title" align="center" title="客户名称"></vxe-column>
      <vxe-column field="carframeCode" width="100" show-overflow="title" align="center" title="车架号"></vxe-column>
      <vxe-column field="motorCode" show-overflow="title" align="center" title="电机号" ></vxe-column>
      <vxe-column field="controllerCode" show-overflow="title" align="center" title="控制器"></vxe-column>
      <vxe-column field="chargerCode" width="100" show-overflow="title" align="center" title="充电器"></vxe-column>
      <vxe-column field="batteryCode" width="100" show-overflow="title" align="center" title="DCH(电池号)"></vxe-column>
      <vxe-column field="quantity" show-overflow="title" align="center" title="工单数量" ></vxe-column>
      <vxe-column field="unitOfMeasure" show-overflow="title" align="center" title="单位"></vxe-column>
      <vxe-column show-overflow="title" align="center" title="创建日期">
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime,'{y}-{m}-{d}') }}</span>
        </template>
      </vxe-column>
      <vxe-column field="createBy"  show-overflow="title" align="center" title="创建人"></vxe-column>
      <vxe-column field="remark"  show-overflow="title" align="center" title="备注"></vxe-column>
      <!-- <vxe-column show-overflow="title" align="center" title="操作">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </vxe-column> -->
    </vxe-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编号" prop="workorderCode">
              <el-input v-model="form.workorderCode" placeholder="请输入工单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品单号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请输入产品单号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品规格" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入产品规格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售订单号" prop="handleUser">
              <el-input v-model="form.handleUser" placeholder="请输入销售订单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车架号" prop="carframeCode">
              <el-input v-model="form.carframeCode" placeholder="请输入车架号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电机号" prop="motorCode">
              <el-input v-model="form.motorCode" placeholder="请输入电机号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="控制器" prop="controllerCode">
              <el-input v-model="form.controllerCode" placeholder="请输入控制器" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充电器" prop="chargerCode">
              <el-input v-model="form.chargerCode" placeholder="请输入充电器" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="DCH(电池号)" prop="batteryCode">
              <el-input v-model="form.batteryCode" placeholder="请输入DCH(电池号)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单数量" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" placeholder="请输入工单数量" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarframe } from "@/api/mes/pro/frameList";
export default {
  name: 'FrameList',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: undefined,
        orderName: undefined,
        productCode: undefined,
        productName: undefined,
        createTime: undefined,
        clientName: undefined,
      },
      tableData: [],
      total: 0,
      columnConfig: {
        drag: true,
        resizable: true,
        storage: true,
        sortConfig: {
          trigger: 'cell',
          remote: true
        },
        align: 'center',
        headerAlign: 'center'
      },
      rowConfig: {
        drag: true,
        keyField: 'id',
        isHover: true,
      },
      ids: [],
      title: '',
      open: false,
      form: {},
      rules: {
        workorderCode:[
          { required: true, message: '请输入工单号', trigger: 'blur' }
        ],
        productCode:[
          { required: true, message: '请输入产品编码', trigger: 'blur' }
        ],
        productName:[
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        carframeCode:[
          { required: true, message: '请输入车架号', trigger: 'blur' }
        ],
        motorCode:[
          { required: true, message: '请输入电机号', trigger: 'blur' }
        ],
        controllerCode:[
          { required: true, message: '请输入控制器', trigger: 'blur' }
        ],
        batteryCode:[
          { required: true, message: '请输入DCH(电池号)', trigger: 'blur' }
        ],
      },
      timeValue: this.getDefaultDateRange(),
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      this.queryParams.startTime = this.timeValue[0]
      this.queryParams.endTime = this.timeValue[1]
      listCarframe(this.queryParams).then(res=>{
        this.tableData = res.rows
        this.total = res.total
      })
    },
    handleQuery(){
      this.queryParams.pageNum = 1
      console.log(this.timeValue);
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workorderCode: undefined,
        orderName: undefined,
        productCode: undefined,
        productName: undefined,
        createTime: undefined,
        clientName: undefined,
      }
      this.timeValue = this.getDefaultDateRange()
      this.getList()
    },
    handleAdd(){
      this.open = true
      this.title = '添加车架单'
    },
    handleDelete(){

    },
    handleUpdate(){

    },
    handleExport(){

    },
    handleInput(){

    },
    reset(){
      this.form = {
        workorderCode: undefined,
        orderName: undefined,
        productCode: undefined,
        productName: undefined,
        createTime: undefined,
        clientName: undefined,
        carframeCode: undefined,
        motorCode: undefined,
        controllerCode: undefined,
        chargerCode: undefined,
        batteryCode: undefined,
        unitOfMeasure: undefined,
      }
      this.$refs.form.resetFields()
    },
    checkboxChangeEvent(checked){
      this.ids = checked.records.map(item => item.rtId)
      console.log(checked.records, this.ids);
    },
    checkboxAll(checked){
      this.ids = checked.records.map(item => item.rtId)
      console.log(checked.records, this.ids)
    },
    submitForm(){

    },
    cancel(){

    },
    getDefaultDateRange() {
      const today = new Date();
      const thirtyDaysAgo = new Date();
      thirtyDaysAgo.setDate(today.getDate() - 30);
      const formatDate = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      };
      return [formatDate(thirtyDaysAgo), formatDate(today)];
    },
  }
}
</script>

<style lang="scss" scoped>

</style>